# -*- coding: utf-8 -*-

import json, struct, gzip, time, random, hashlib, uuid
#from urllib import requests
import requests
from io import BytesIO

g_host = "https://upl.baidu-int.com"

def generate_sign(username):
    pass

def get_md5_value(str):
    my_md5 = hashlib.md5()
    my_md5.update(str.encode('utf8'))
    my_md5_Digest = my_md5.hexdigest()
    return my_md5_Digest

def gzip_compress(buf):
    out = BytesIO()
    with gzip.GzipFile(fileobj=out, mode="w") as f:
        f.write(buf.encode())
    return out.getvalue()


def request_model_train_hotwords(pid, cuid, delete=False):
    slot = {}
    slot["slot_name"] = "hotwords"
    slot["version"] = 1
    slot["mode"] = 1
    slot["hot_words_type"] = "HOT_WORDS_TYPE_SPORTS"
    slot_value = []
    # slot_value.append("热词猪猪侠")
    # slot_value.append("热词西山壹号院")
    # slot_value.append("热词王五")
    # slot_value.append("热词刘伟民")
    # slot_value.append("热词问啐啑啒启啔啕啖啖啘啙啚啛啜啝哑启啠啡啢")
    # slot_value.append("热词刘为民")
    slot_value.append("往上滑")
    slot_value.append("往下滑")
    slot_value.append("精灵3号的价格")
    slot["hot_words"] = slot_value
    slot_string = json.dumps(slot)
    body = "glb=" + str(uuid.uuid1())
    body = body + "&upload_pid=" + str(pid)
    body = body + "&uid=" + cuid
    body = body + "&sign=" + get_md5_value(str(pid) + "ABCD" + slot_string)
    body = body + "&content=" + slot_string
    if delete:
        body = body + "&delete=1"

    response = requests.post(g_host + "/hotwords_ctx", data=body)
    print(response.content)



if __name__ == "__main__":
    # cuid fake_cuid_rd_test_car_1190
    # 768bbd04ada7be9ce4e9f338d0e1f80762fdca49d95f12d39151b467f39ca77a
    request_model_train_hotwords(1183, "944d1d0b-1cbc-4ddb-a8eb-be032f05101f_93018098")

